package com.zlkj.pro.modular.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.consts.ProjectConstants;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.api.model.dto.VersionDTO;
import com.zlkj.pro.modular.api.model.vo.VersionVO;
import com.zlkj.pro.modular.api.service.VersionService;
import com.zlkj.pro.modular.common.entity.VersionEntity;
import com.zlkj.pro.modular.common.mapper.VersionMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 孙铭泽
 * @date 2022/9/24
 * @time 12:36
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class VersionServiceImpl extends ServiceImpl<VersionMapper, VersionEntity> implements VersionService {

    @Override
    public VersionVO checkForVersionUpdate(VersionDTO versionDTO) {
        String urlType = versionDTO.getUrlType();
        LambdaQueryWrapper<VersionEntity> versionEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        versionEntityLambdaQueryWrapper.eq(VersionEntity::getUrlType, urlType);
        versionEntityLambdaQueryWrapper.eq(VersionEntity::getStatusFlag, ProjectConstants.StatusFlag.ENABLE);
        versionEntityLambdaQueryWrapper.orderByDesc(VersionEntity::getCode);
        versionEntityLambdaQueryWrapper.last("LIMIT 1");
        VersionEntity versionEntity = super.getOne(versionEntityLambdaQueryWrapper);
        return ConvertUtil.turn(versionEntity, VersionVO.class);
    }
}
